package com.garlic.face.jlf.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;
import com.garlic.face.jlf.base.BaseActivity;
import com.garlic.face.jlf.presenter.PayWayContract;
import com.garlic.face.jlf.presenter.PayWayPresenter;
import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.utils.LogUtils;
import com.garlic.face.jlf.utils.ToastUtils;
import com.garlic.face.jlf.utils.Tools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;
import java.util.Locale;

/**
 * @author wenhui02.liu
 * @date 2018/8/9.
 */

public class PayWayActivity extends BaseActivity implements PayWayContract.View {

    private static final String TAG = "PayWayActivity";
    PayWayContract.Presenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_activity);
        mPresenter = new PayWayPresenter(this);
        ImageView shualian = (ImageView) findViewById(R.id.scanFaceToPay);
        ImageView saoma = (ImageView) findViewById(R.id.scanCodeToPay);
        ImageView cardpay = findViewById(R.id.cardToPay);

        if (Means.getInstance().getRec_Tran().getAmount() >= App.NO_SUPPORT_FACE_PAY) {
            shualian.setImageResource(R.mipmap.payment_shualian_disabled);
        } else {
            shualian.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    LogUtils.d("发起刷脸支付流程", true);
                    mPresenter.smilePay();
                }
            });
        }

        saoma.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PayWayActivity.this, BarCodePayActivity.class);
                startActivity(intent);
            }
        });

        cardpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PayWayActivity.this, CardPayActivity.class);
                startActivity(intent);
            }
        });


        findViewById(R.id.cancle_payway).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                View root = View.inflate(PayWayActivity.this, R.layout.close_dialog, null);
                AlertDialog.Builder builder = new AlertDialog.Builder(PayWayActivity.this).setView
                        (root).setCancelable(false);
                final Dialog dialog = builder.create();
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                root.findViewById(R.id.goto_agin).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                root.findViewById(R.id.cancel_action).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        showProgress();
                        mPresenter.cancelTransaction();
                    }
                });

            }
        });

        if(Tools.getLanguage().equals(Locale.US.toString())){
            saoma.setImageResource(R.mipmap.payment_saoma_en);
            if (Means.getInstance().getRec_Tran().getAmount() >= App.NO_SUPPORT_FACE_PAY) {
                shualian.setImageResource(R.mipmap.payment_shualian_disabled_en);
            }else{
                shualian.setImageResource(R.mipmap.payment_shualian_en);
            }
            cardpay.setImageResource(R.mipmap.payment_card_en);
        }else {
            saoma.setImageResource(R.mipmap.payment_saoma);
            if (Means.getInstance().getRec_Tran().getAmount() >= App.NO_SUPPORT_FACE_PAY) {
                shualian.setImageResource(R.mipmap.payment_shualian_disabled);
            }else{
                shualian.setImageResource(R.mipmap.payment_shualian);
            }
            cardpay.setImageResource(R.mipmap.payment_card);
        }
        App.playSound(getResources().getString(R.string.sound_choose_pay_type));
    }


    public final static int TOAST_SUCCESS = 0;
    public final static int TOAST_INFO = 1;
    public final static int TOAST_FAIL = 2;

    /**
     * 发起刷脸支付请求.
     *
     * @param txt toast文案
     */
    @Override
    public void promptText(final int tipType, final String txt) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                switch (tipType) {
                    case TOAST_SUCCESS:
                        ToastUtils.toastShow(txt, ToastUtils.SUCCESS);
                        break;
                    case TOAST_INFO:
                        ToastUtils.toastShow(txt, ToastUtils.INFO);
                        break;
                    case TOAST_FAIL:
                        ToastUtils.toastShow(txt, ToastUtils.ERROR);
                        break;

                }
            }
        });
    }

    @Override
    public void gotoMain() {
        finish();
    }

    @Override
    public void toSuccssActivity() {
        Intent intent = new Intent(PayWayActivity.this, PaySuccessActivity.class);
        intent.putExtra("scene", "facepay");
        startActivity(intent);
        finish();
    }

    @Override
    public void toFailureActivity(String retmsg, String actionType) {
        Intent intent = new Intent(PayWayActivity.this, PayFailActivity.class);
        intent.putExtra("error_message", retmsg);
        intent.putExtra("action_type", actionType);
        startActivity(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveResponse(JSONObject jsonObject) {
        LogUtils.d("收到service消息 " + jsonObject.toString(), true);
        mPresenter.parseResponse(jsonObject);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
        App.getSoundMgr().stopPlay();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.destoryZoloz();
        mPresenter.destoryMXdevice();
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
            return true;
        }else {
            return super.dispatchKeyEvent(event);
        }
    }
}

